const express = require('express')
const next = require('next')
const routes = require('./routes')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const handler = routes.getRequestHandler(app)
const devProxy = {
  '/api': {
    target: 'http://api.kanzhun-inc.com/mock/129/api/',
    pathRewrite: {'^/api': '/'},
    changeOrigin: true
  }
}
app.prepare()
.then(() => {
  const server = express()
 // server.use(handler)
  if (dev && devProxy) {
    const proxyMiddleware = require('http-proxy-middleware')
    Object.keys(devProxy).forEach(function (context) {
        server.use(proxyMiddleware(context, devProxy[context]))
    })
  }
  //server.use(handler)
  server.get('*', (req, res) => {
    return handler(req, res)
  })
 
  server.listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')
  })
  

})







